unit MiniHTTP;

interface

uses
  Windows, WinInet, MiniSysUtils;

function LoadURL(URL: String; UserAgent: String): String;
function UrlEncode(const DecodedStr: String; Pluses: Boolean): String;

implementation

function UrlEncode(const DecodedStr: String; Pluses: Boolean): String;
var
  I: Integer;
begin
  Result := '';
  if Length(DecodedStr) > 0 then
    for I := 1 to Length(DecodedStr) do
    begin
      if not (DecodedStr[I] in ['0'..'9', 'a'..'z', ':', '/', '_', '.', '-', 
                                       'A'..'Z', ' ']) then
        Result := Result + '%' + IntToHex(Ord(DecodedStr[I]), 2)
      else if not (DecodedStr[I] = ' ') then
        Result := Result + DecodedStr[I]
      else
        begin
          if not Pluses then
            Result := Result + '%20'
          else
            Result := Result + '+';
        end;
    end;
end;

function LoadURL(URL: String; UserAgent: String): String;
var
  IOpen, IURL: HINTERNET;
  Read: Cardinal;
  Msg: string;
begin
  Result := '';
  try
    IOpen := InternetOpen(
               PChar(UserAgent),
               INTERNET_OPEN_TYPE_PRECONFIG, '', '',
               INTERNET_FLAG_NEED_FILE
             );
    if IOpen <> nil then
    try
      IURL := InternetOpenUrl(IOpen, PAnsiChar(URL), nil, 0,
                INTERNET_FLAG_NO_UI or INTERNET_FLAG_NO_CACHE_WRITE, 0);
      if IURL <> nil then
      try
        SetLength(Msg, 4096);
        repeat
          if InternetReadFile(IURL, @Msg[1], 4096, Read)
          then
            Result := Result + Copy(Msg, 1, Read)
          else
            Break;
        until Read = 0;
      finally
        InternetCloseHandle(IURL);
      end;
    finally
      InternetCloseHandle(IOpen);
    end;
  except
  end;
end;

end.
